/*******************************************************************************
* Copyright (c) 2010, 2018 西安秦晔信息科技有限公司
* Licensed under the Apache License, Version 2.0 (the "License");
*    you may not use this file except in compliance with the License.
*    You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*    Unless required by applicable law or agreed to in writing, software
*    distributed under the License is distributed on an "AS IS" BASIS,
*    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*    See the License for the specific language governing permissions and
*    limitations under the License.
*******************************************************************************/
package com.qinyeit.serviceapp.repository.dishes;

import com.qinyetech.springstage.core.repository.BaseRepository;
import com.qinyeit.serviceapp.entity.Dishes;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

/** 
 * ClassName: DishesRepository <br/> 
 * Function: 菜品 CRUD <br/> 
 * date: 2018-11-23 11:45:37 <br/> 
 * 
 * @author liuwenjin
 * @version 1.0
 * @since JDK 1.8
 */
public interface DishesRepository extends BaseRepository<Dishes, Long> {

    @Transactional
    @Modifying
    @Query("DELETE FROM DishesMakeRelation WHERE dishesId=?1")
    void deleteMakes(Long dishesId);

    @Transactional
    @Modifying
    @Query("DELETE FROM DishesSpecificationRelation Where dishesId=?1")
    void deleteSpecs(Long dishesId);

    @Transactional
    @Modifying
    @Query("DELETE FROM DishesFeedRelation Where dishesId=?1")
    void deleteFeeds(Long dishesId);

    /**
     * 查找套餐明细id
     * @param packId
     * @return
     */
    @Query(value="SELECT DISTINCT d.id FROM dishes_package_details d LEFT JOIN dishes_package_groups g ON d.group_id=g.id WHERE g.pack_id=?1",nativeQuery = true)
    Long[] findByPackId(Long packId);


    /**
     * 删除套餐明细
     * @param detailIds
     */
    @Transactional
    @Modifying
    @Query("DELETE FROM DishesPackageDetail WHERE id in ?1")
    void deletePackageDetailsByPackageIds(Long ...detailIds);

    /**
     * 删除套餐 的 套餐组
     * @param dishesId
     */
    @Transactional
    @Modifying
    @Query("DELETE FROM DishesPackageGroup WHERE packId=?1")
    void deletePackageGroupByDishes(Long dishesId);


    /**
     * 查找成本卡对应的成本明细id
     * @param dishesId
     * @return
     */
    @Query(value="SELECT DISTINCT id from material_cost_details where cost_id in(SELECT id from material_costs where dishes_id = ?1)",nativeQuery = true)
    Long[] findByCostDetailsId(Long dishesId);

    /**
     * 删除成本卡明细
     * @param detailIds
     */
    @Transactional
    @Modifying
    @Query("DELETE FROM MaterialCostDetail WHERE id in ?1")
    void deleteCostDetailsByIds(Long ...detailIds);

    /**
     * 删除成本卡
     * @param dishesId
     */
    @Transactional
    @Modifying
    @Query("DELETE FROM MaterialCost WHERE dishesId=?1")
    void deleteCostByDishes(Long dishesId);


}